package com.dhcc.DRGsHosp.application.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.inject.Named;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.transaction.annotation.Transactional;

import com.dhcc.DRGsHosp.application.OperationAnalysisApplication;
import com.dhcc.DRGsHosp.core.domain.IndexCal;

/**
 * 手术分析
 * @ClassName: OperationAnalysisApplicationImpl  
 * @Description ：    
 * @Date: 2017年12月22日 下午4:07:20  
 * @Author ChenHanlin 
 * @Version 3.0.2 DHC-DRGs
 */
@Named
@Transactional
public class OperationAnalysisApplicationImpl implements OperationAnalysisApplication {
	@Autowired
	private NamedParameterJdbcTemplate jdbcTempSeverice;
	
	private static final Logger logger = LoggerFactory.getLogger(OperationAnalysisApplicationImpl.class);
	
	@Override
	public List<Map<String, Object>> executeQueryForList(String sql,Object obj) {
		SqlParameterSource paramSource = new BeanPropertySqlParameterSource(obj);
		List<Map<String, Object>> mapLsit = new ArrayList<>() ;
		try {
			mapLsit = jdbcTempSeverice.queryForList(sql, paramSource)  ;
		} catch (Exception e) {
			e.printStackTrace();
			System.err.println(sql);
			logger.error(sql);
		}
		
		return mapLsit;
	}
	
	
	@Override
	@Cacheable(value = "drgsCaseDetail3.1.1", key = "'OperationAnalysisApplicationImplFindCaseCount' + #index.pType + #index.pardepgroupCode + #index.deps + #index.pardepCode + #index.parwardCode  + "
			+ "#index.pargroupName + #index.protomedicus + #index.resident + #index.physician + #index.doctorCode + "
			+ "#index.parmdcCode + #index.paradrgCode + #index.pardrgCode  + #index.typeOne  +"
			+ "#index.dateFrom + #index.dateTo + #index.stdId + #index.manageYear + #index.typeTwo  + "
			+ "#index.type2 + #index.quotaCode + #index.msCode+ #index.mdcs + #index.opeLevel ")
	public int findCaseCount(IndexCal index) {
		String caseCountHead = "SELECT COUNT(*) caseCount FROM ( ";
		String caseCountFoot = " ) ";
		SqlParameterSource paramSource = new BeanPropertySqlParameterSource(index);
		int cnt = 0 ;
		try {
			cnt = jdbcTempSeverice.queryForObject(caseCountHead + index.getSqlStr() + caseCountFoot, paramSource, Integer.class);
		} catch (Exception e) {
			e.printStackTrace();
			return cnt ;
		}
		return cnt ;
	}
	
	
	@Override
	@Cacheable(value = "drgsCaseDetail3.1.1", key = "'OperationAnalysisApplicationImplFindCaseList' + #page + #pageSize +#orderColumn+ #orderMethod+ "
			+ "#index.pType + #index.pardepgroupCode + #index.deps + #index.pardepCode + #index.parwardCode  + "
			+ "#index.pargroupName + #index.protomedicus + #index.resident + #index.physician + #index.doctorCode + "
			+ "#index.parmdcCode + #index.paradrgCode + #index.pardrgCode  + #index.typeOne  +"
			+ "#index.dateFrom + #index.dateTo + #index.stdId + #index.manageYear + #index.typeTwo  + "
			+ "#index.type2 + #index.quotaCode + #index.msCode+ #index.mdcs + #index.opeLevel ")
	public List<Map<String, Object>> findCaseList(IndexCal index,int page, int pageSize, String orderColumn,
			String orderMethod) {
		// 不分页,每页默认99999999
		if (pageSize == -1) {
			pageSize = 99999999;
		}
		String pageSqlHead = "SELECT * FROM (SELECT PAGERESULT.*, ROWNUM RN FROM (";

		String pageSqlFoot = " ORDER BY " + orderColumn + " " + orderMethod + ") PAGERESULT ) WHERE RN > " + page
				+ " AND RN <= " + (page + pageSize);
		List<Map<String,Object>> listMap = executeQueryForList(pageSqlHead+index.getSqlStr()+pageSqlFoot,index) ;
		return listMap;
	}
	
}
